<template>
  <div>
    <div v-show="(!isEdit&&!isDetail)" class="page-index">
      <div class="page-left">
        <ClassifyTreeSilder @nodeClick="nodeClick" ref="treeSilder" />
      </div>
      <div class="page-right">
        <div class="table-search">
          <el-form inline>
            <el-form-item label="姓名">
              <el-input v-model="queryParams.name" size="mini" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="所在支部">
              <ClassifyTreeSelect @input="getBranchId" @getValue="getBranchNode" :value="queryParams.branchId" size="mini" />
            </el-form-item>
            <el-form-item label="党内职务">
              <el-select v-model="queryParams.partyPositionsId" size="mini" placeholder="请选择" >
                <el-option
                  v-for="item in positionList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.name"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="党员身份">
              <el-select v-model="queryParams.partyMember" size="mini" placeholder="请选择" >
                <el-option
                  v-for="(item,index) in partyMemberList"
                  :key="index"
                  :value="index"
                  :label="item"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button size="mini" type="primary" @click="search">搜索</el-button>
              <el-button size="mini" @click="reset">重置</el-button>
            </el-form-item>
          </el-form>
          <el-form inline>
            <el-form-item>
              <el-button size="mini" type="primary" @click="openAdd">新增</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="table-box">
          <el-table
            :data="tableData"
            height="100%"
          >
            <el-table-column type="index" label="序号" align="center" width="50" />
            <el-table-column prop="name" label="姓名" align="center" width="150" />
            <el-table-column prop="branchName" label="所在支部" align="center" width="150" />
            <el-table-column prop="partyPositionsName" label="党内职务" align="center" width="120" />
            <el-table-column prop="contact" label="联系方式" align="center" width="150" />
            <el-table-column prop="positionLevelName" label="职位级别" align="center" min-width="150" />
            <el-table-column prop="partyMember" label="党员身份" align="center" width="120">
              <template slot-scope="scope">
                {{ partyMemberList[scope.row.partyMember] }}
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="120">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  size="mini"
                  @click="openDetail(scope.row)"
                >查看</el-button>
                <el-button
                  type="text"
                  size="mini"
                  @click="openEdit(scope.row)"
                >编辑</el-button>
                <el-button
                  type="text"
                  size="mini"
                  style="color:#F56C6C;"
                  @click="openDelete(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <pagination
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getData"
        />
      </div>
    </div>
    <addOrEdit v-show="isEdit" ref="addOrEdit" @getData="getData" @goback="editGoback" />
    <detailPage v-show="isDetail" ref="detailPage" @detailToEdit="detailToEdit" @goback="detailGoback" />
  </div>
</template>
<script>
import { queryTreeByCondition, queryById, deleteById, queryPositionsList } from '@/api/partyMemberManagement/memberInformationManagement'
import ClassifyTreeSilder from '@/components/classifyTree/classifyTreeSilder.vue'
import ClassifyTreeSelect from '@/components/classifyTree/classifyTreeSelect.vue'
import addOrEdit from './components/addOrEdit'
import detailPage from './components/detail'
export default {
  components: {
    ClassifyTreeSilder,
    ClassifyTreeSelect,
    addOrEdit,
    detailPage
  },
  data () {
    return {
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        name: '',
        partyMember: '',
        branchId: '',
        partyPositionsId: ''
      },
      total: 0,
      tableData: [],
      positionList: [],
      isDetail: false,
      isEdit: false,
      partyMemberList: ['正式党员', '组织关系转出', '失联', '清退', '死亡', '开除党籍']
    }
  },
  mounted () {
    this.getData()
  },
  methods: {
    getData () {
      queryTreeByCondition(this.queryParams).then((res) => {
        this.tableData = res?.data?.records || []
        this.total = res?.data?.total || 0
      })
    },
    getPositionList () {
      queryPositionsList().then((res) => {
        this.positionList = res?.data || []
      })
    },
    search () {
      this.queryParams.pageNo = 1
      this.getData()
    },
    openAdd () {
      this.isEdit = true
      this.$refs.addOrEdit.init({})
    },
    openEdit (row) {
      queryById({ id: row.id }).then((res) => {
        this.isEdit = true
        this.$refs.addOrEdit.init(res?.data || row)
      })
    },
    nodeClick (node) {
      this.queryParams.branchId = node.id
      this.search()
    },
    getBranchNode (node) {
      this.queryParams.branchId = node?.id || null
    },
    getBranchId (id) {
      // console.log('id', id)
    },
    openDelete (row) {
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById({ id: row.id }).then((res) => {
          this.$message.success('删除成功')
          this.getData()
        })
      })
    },
    openDetail (row) {
      queryById({ id: row.id }).then((res) => {
        this.isDetail = true
        this.$refs.detailPage.init(res?.data || row)
      })
    },
    editGoback (isQuery) {
      this.isEdit = false
      if (isQuery) {
        this.getData()
      }
    },
    detailGoback () {
      this.isDetail = false
    },
    detailToEdit (row) {
      this.isDetail = false
      this.openEdit(row)
    },
    reset () {
      this.queryParams.name = ''
      this.queryParams.partyMember = ''
      this.queryParams.branchId = ''
      this.queryParams.partyPositionsId = ''
      this.search()
    }
  }
}
</script>
<style scoped lang="scss">
.page-index {
  display: flex;
  padding-top: 24px;
  .page-left {
    height: calc(100vh - 130px);
    overflow-y: auto;
    width: 300px;
  }
  .page-right {
    flex: 1;
    .table-search {
      display: flex;
      justify-content: space-between;
      padding-left:16px;
    }
    .table-box {
      height: calc(100vh - 280px);
    }
  }
}

</style>
